package ooday04test;

public class PolymorphicDemo {
    public static void main(String[] args) {
        //
        Animal[] animals=new Animal[4];
        animals[0] =new Dog("旺财",3,"白色");
        animals[1] =new Dog("小黄",2,"黑色");
        animals[2] =new Fish("小花",1,"蓝色");
        animals[3] =new Chick("小鸡",1,"白色");

        //遍历数组
        for (int i = 0; i < animals.length; i++){
            System.out.println(animals[i].name);
            animals[i].eat();
            animals[i].drink();
            //动态绑定机制
            if (animals[i] instanceof Dog){
                Dog dog=(Dog) animals[i];
                dog.lookHome();
            }
            if (animals[i] instanceof Chick){
                Chick chick=(Chick) animals[i];
                chick.layEggs();
            }
            if (animals[i] instanceof Swim){
                Swim s=(Swim) animals[i];
                s.swim();
            }
        }
    }
}
